
export default class TimeUtil {
    /**
     * 获取当前时间戳
     */
    public static getTime(): number {
        // return new Date().getTime();
        return Date.now();
    }

    /**
     * 获得某月的天数 0~11月份从0开始
     * -@param nowYear
     * -@param nowMonth
     */
    public static getMonthDays(nowYear: number, nowMonth: number) {
        nowYear += (nowYear < 2000) ? 1900 : 0;
        let monthStartDate: any = new Date(nowYear, nowMonth, 1);
        let monthEndDate: any = new Date(nowYear, nowMonth + 1, 1);
        let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
    }

    /**
     * 格式化日期：yyyy-MM-dd
     * -@param date
     */
    public static formatDate(date) {
        let myyear = date.getFullYear();
        let mymonth = date.getMonth() + 1;
        let myweekday = date.getDate();

        if (mymonth < 10) {
            mymonth = "0" + mymonth;
        }
        if (myweekday < 10) {
            myweekday = "0" + myweekday;
        }
        return (myyear + "-" + mymonth + "-" + myweekday);
    }

    /**
     * 时间格式化
     *
     * @param {number} millisecond
     * @returns {number[]}
     * @memberof GameUtil
     */
    public static formatTime(millisecond: number): number[] {
        let sec = Math.floor(millisecond / 1000);
        let min = Math.floor(sec / 60);
        sec = sec % 60;
        let hour = Math.floor(min / 60);
        min = min % 60;
        const day = Math.floor(hour / 24);
        hour = hour % 24;
        let result = [day, hour, min, sec];
        return result;
    }

    /**
     * 时间戳格式转化  00:00:00
     * -@param time 时间戳
     * -@returns 
     */
    public static formatHour(time: number): string {
        let second: number = time % 60;
        let minute: number = Math.floor(time / 60 % 60);
        let hour: number = Math.floor(time / (60 * 60));
        let minuteString: string = minute > 9 ? minute + "" : "0" + minute;
        let secondString: string = second > 9 ? second + "" : "0" + second;
        let hourString: string = hour > 9 ? hour + "" : "0" + hour;
        return hourString + ":" + minuteString + ":" + secondString;
    }

    /**
     * 时间戳格式转化  00:00
     * -@param time 时间戳
     * -@returns 
     */
    public static formatMinute(time: number): string {
        let second: number = time % 60;
        let minute: number = Math.floor(time / 60 % 60);
        let minuteString: string = minute > 9 ? minute + "" : "0" + minute;
        let secondString: string = second > 9 ? second + "" : "0" + second;
        return minuteString + ":" + secondString;
    }


    /**
     * 小于10前面补0
     * @param value 
     * @returns 
     */
    public static fillZero(value: number) {
        return value < 10 ? '0' + value : value;
    }

    /**
     * 把时间戳 转为 年-月-日 时:分:秒 格式
     * 转换后   2020-04-07 18:08:58
     * @param ts 
     * @returns 
     */
    public static dateTime(ts: number) {
        const time = new Date(ts);
        const year = time.getFullYear();
        const month = time.getMonth() + 1;
        const day = time.getDate();
        const hour = time.getHours();
        const minute = time.getMinutes();
        const second = time.getSeconds();
        
        return year + '-' + this.fillZero(month) + '-' + this.fillZero(day) + ' ' + this.fillZero(hour) + ':' + this.fillZero(minute) + ':' + this.fillZero(second)
    }

    /**
     * 把时间戳 转为 年-月-日 时:分:秒 格式
     * @param value 
     */
    public static changeTimeStamp(value: number) {
        if (value) {
            return;
        }
        const date = new Date(value);
        // 有三种方式获取
        const time1 = date.getTime();
        const time2 = date.valueOf();
        const time3 = Date.parse(`${date}`);
        console.log(time1);
        console.log(time2);
        console.log(time3);
    }

    /**
     * 时间比较( 要求日期格式：yyyy-mm-dd hh:mi:ss)
     * 
     * @param firstDate 
     * @param lastDate 
     * @returns 
     */
    public static judgeDate(beginTime: string, endTime: string) {
        let beginTimes = beginTime.substring(0, 10).split('-');
        let endTimes = endTime.substring(0, 10).split('-');

        beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
        endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19);

        let result = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
        if (result < 0) {
            console.log('前一天 大!');
        } else if (result > 0) {
            console.log('前一天 小!');
        } else if (result == 0) {
            console.log('两天相等!');
        } else {
            return 'exception';
        }
    }

    /**
     * date 代表指定的日期，格式：2018-09-27
     * day 传-1表始前一天，传1表始后一天
     * 获取指定日期的前一天，后一天
     * 
     * @param date
     * @param day 
     * @returns 
     */
    public static getNextDate(date, day) {
        let time = new Date(date);
        time.setDate(time.getDate() + day);
        let y = time.getFullYear();
        let m = time.getMonth() + 1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
        let d = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
        return y + "-" + m + "-" + d;
    };
}